package ru.amse.stroganova.ui.command;

import ru.amse.stroganova.presentation.EdgePresentation;
import ru.amse.stroganova.presentation.GraphPresentation;
import ru.amse.stroganova.presentation.VertexPresentation;
import ru.amse.stroganova.ui.visual.GraphElementSelection;

class EdgeEndEditCommand implements Command {

	private final EdgePresentation oldEdge;
	
	private final EdgePresentation newEdge;
	
	private final GraphPresentation graphPresentation;
	
	private final GraphElementSelection graphSelection;
	
	EdgeEndEditCommand(EdgePresentation edge, VertexPresentation sourcePr, VertexPresentation destinPr, GraphPresentation graphPresentation, GraphElementSelection graphSelection) {
		this.graphSelection = graphSelection;
		this.graphPresentation = graphPresentation;
		oldEdge = edge;
		newEdge = new EdgePresentation(sourcePr, destinPr, graphPresentation.isDirected(), graphPresentation.isWeighted());
		newEdge.setWeight(oldEdge.getWeight());
	}
	
	public void execute() {
		graphSelection.unselect();
		graphPresentation.pauseFiringChanges();
		graphPresentation.removeEdge(oldEdge);
		graphPresentation.addEdge(newEdge);
		graphPresentation.resumeFiringChanges();
		graphSelection.selectEdge(newEdge);
	}

	public void undo() {
		graphSelection.unselect();
		graphPresentation.pauseFiringChanges();
		graphPresentation.removeEdge(newEdge);
		graphPresentation.addEdge(oldEdge);
		graphPresentation.resumeFiringChanges();
		graphSelection.selectEdge(oldEdge);
	}
}
